<?php
require("../../../../../libraries/config.php");
require("../../../../../libraries/dbclass2.php");

function xcrud_remove_post_report($id){
	$db=new db;
	$db->connect();
	$db->query('select * from index_post where id='.$id);
	$rc=$db->fetchOne();
		$pid=$rc['post_id'];
		$db->nonquery('delete from index_post where id='.$id);
	if($rc['is_image']==1){
	
		$db->nonquery('delete from images where post_id='.$pid);
		$db->nonquery('delete from post_image where id='.$pid);
	}else
	if($rc['is_video']==1){
		$db->nonquery('delete from post_media where id='.$pid);
	}else
	if($rc['is_text']==1){
		$db->nonquery('delete from post_text where id='.$pid);
	}
	$sql="update users set report=report+1 where username='".$rc['username']."'";
	$db->nonquery($sql);
	
	
	$db->close();


}

function xcrud_remove_post($id){
	$db=new db;
	$db->connect();
	$db->query('select * from index_post where id='.$id);
	$rc=$db->fetchOne();
		$pid=$rc['post_id'];
		$db->nonquery('delete from index_post where id='.$id);
	if($rc['is_image']==1){
	
		$db->nonquery('delete from images where post_id='.$pid);
		$db->nonquery('delete from post_image where id='.$pid);
	}else
	if($rc['is_video']==1){
		$db->nonquery('delete from post_media where id='.$pid);
	}else
	if($rc['is_text']==1){
		$db->nonquery('delete from post_text where id='.$pid);
	}
	

	
	
	$db->close();


}


function publish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
		$db->query("select xoa from truyen where id=".(int)$xcrud->get('primary'));
		$r1=$db->row();
		if($r1['xoa']==0){
        $query = 'UPDATE truyen SET `xoa` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
		}else{
			  $query = 'UPDATE truyen SET `xoa` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
		}
        $db->query($query);
    }
}
function unpublish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE truyen SET `xoa` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}
function publish_action2($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE truyen_chuong SET `xoa` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}
function unpublish_action2($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE truyen_chuong SET `xoa` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}

function pass_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
		$db->query("select duyet from truyen where id=".(int)$xcrud->get('primary'));
		$r1=$db->row();
		if($r1['duyet']==0){
        $query = 'UPDATE truyen SET `duyet` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
		}else{
		$query = 'UPDATE truyen SET `duyet` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
		}
		$db->query($query);
    }
}

function clear_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
    $id=$xcrud->get('primary');
	$db=new db;
	$db->connect();
	//$db->nonquery('delete from truyen where id='.$id);
	$db->nonquery('delete from truyen_chuong_link where story_id='.$id);
	$db->nonquery('delete from truyen_chuong where truyen_id='.$id);
	

	$db->nonquery('delete from truyen_chuong_noidung1 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung2 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung3 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung4 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung5 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung6 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung7 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung8 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung9 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung10 where truyen_id='.$id);				
	
	$db->nonquery('delete from truyen_tacgia_fk where truyen_id='.$id);
	$db->nonquery('delete from truyen_theloai_fk where truyen_id='.$id);
	$db->nonquery('delete from truyen_tag_fk where truyen_id='.$id);
	//$db->nonquery('delete from truyensearch where id='.$id);
	$db->close();
    }
}



function delete_report($xcrud)
{
    if ($xcrud->get('postid'))
    {
		
		$db = Xcrud_db::get_instance();
        $query = 'update report_post set xuly=1 WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
	//	xcrud_remove_post_report((int)$xcrud->get('postid'));
		xcrud_remove_post_report($xcrud->get('postid'));
	
		
      
    }
}

function xulytruyen($xcrud)
{
    if ($xcrud->get('primary'))
    {
		
		$db = Xcrud_db::get_instance();
        $query = 'update report_truyen set xuly=1,xuly_username="'.$_SESSION['user']['username'].'" WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
	//	xcrud_remove_post_report((int)$xcrud->get('postid'));
	
		
      
    }
}


function duyetupload($xcrud)
{
    if ($xcrud->get('primary'))
    {
		$db = Xcrud_db::get_instance();
        $query = 'update register_upload set duyet=1 WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
		$query = 'update users set upload=1 WHERE username = "'. mysql_real_escape_string($xcrud->get('user')).'"';
        $db->query($query);
   
    }
}
?>
